package com.example.beautyimage;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.ImageView;

public class ColorMatrix extends AppCompatActivity {
    private ImageView imageView;
    private Bitmap bitmap;
    private GridLayout group;
    private Button btnChange;
    private Button btnReset;

    private int mEtWidth, mEtHeight;
    //对应颜色5*4矩阵
    private EditText[] editTexts = new EditText[20];
    private float[] mColorMatrix = new float[20];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_matrix);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zw);
        imageView = findViewById(R.id.cm_imageview);
        btnChange = findViewById(R.id.btn_change);
        btnReset = findViewById(R.id.btn_reset);
        group = findViewById(R.id.cm_group);
        imageView.setImageBitmap(bitmap);
        //显示20个EditText
        group.post(new Runnable() {
            @Override
            public void run() {
                mEtWidth = group.getWidth() / 5;
                mEtHeight = group.getHeight() / 4;
                addEts();
                //初始化颜色矩阵值
                initMatrix();
            }
        });
        btnChange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMatrix();
                setImageMatrix();
            }
        });
        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initMatrix();
                getMatrix();
                setImageMatrix();


            }
        });
    }

    private void getMatrix() {
        for (int i = 0; i < 20; i++) {
            mColorMatrix[i] = Float.valueOf(editTexts[i].getText().toString());
        }
    }
    //绘制图像
    private void setImageMatrix() {
        Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        android.graphics.ColorMatrix colorMatrix = new android.graphics.ColorMatrix();
        colorMatrix.set(mColorMatrix);

        Canvas canvas = new Canvas(bitmap1);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        imageView.setImageBitmap(bitmap1);
    }

    private void addEts() {
        for (int i = 0; i < 20; i++) {
            EditText editText = new EditText(ColorMatrix.this);
            editTexts[i] = editText;
            group.addView(editText, mEtWidth, mEtHeight);
        }
    }

    private void initMatrix() {
        for (int i = 0; i < 20; i++) {
            if (i % 6 == 0) {
                editTexts[i].setText(String.valueOf(1));
            } else {
                editTexts[i].setText(String.valueOf(0));
            }
        }
    }
}
